#define _CRT_SECURE_NO_WARNINGS 1
#include "ObjectPool.h"
#include "ConcurrentAlloc.h"
void Alloc1() {
	for (int i = 0; i < 5; i++)
	{
		void* ptr = ConcurrentAlloc(6);
	}
}
void Alloc2() {
	for (int i = 0; i < 5; i++)
	{
		void* ptr = ConcurrentAlloc(6);
	}
}
void TLSTest() {
	std::thread thread1(Alloc1);
	std::thread thread2(Alloc2);
	thread1.join();
	thread2.join();
}
void TestConurrentAlloc1() {
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(1);
	void* p4 = ConcurrentAlloc(7);
	void* p5 = ConcurrentAlloc(8);
	void* p6 = ConcurrentAlloc(7);
	void* p7 = ConcurrentAlloc(8);

	cout << p1 << endl;
	cout << p2 << endl;
	cout << p3 << endl;
	cout << p4 << endl;
	cout << p5 << endl;

	ConcurrentFree(p1);
	ConcurrentFree(p2);
	ConcurrentFree(p3);
	ConcurrentFree(p4);
	ConcurrentFree(p5);
	ConcurrentFree(p6);
	ConcurrentFree(p7);
}
void TestConurrentAlloc2() {
	for (int i = 0; i < 1024; i++)
	{
		void* p1 = ConcurrentAlloc(6);
		cout << i + 1 << " : " << p1 << endl;
	}
	void* p1 = ConcurrentAlloc(6);
	cout << p1 << " : " << p1 << endl;
	void* p2 = ConcurrentAlloc(6);
	cout << p2 << " : " << p2 << endl;
}
void TestAddressShift() {
	PAGE_ID id1 = 2000;
	PAGE_ID id2 = 2001;
	char* p1 = (char*)(id1 << PAGE_SHIFT);
	char* p2 = (char*)(id2 << PAGE_SHIFT);
	while (p1 < p2)
	{
		cout << (void*)p1 << " : " << ((PAGE_ID)p1 >> PAGE_SHIFT) << endl;
		p1 += 8;
	}
}
void MultiThreadAlloc1() {
	std::vector<void*> v;
	for (int i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}
	for (auto& e : v)
	{
		ConcurrentFree(e);
	}
}
void MultiThreadAlloc2() {
	std::vector<void*> v;
	for (int i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}
	for (auto& e : v)
	{
		ConcurrentFree(e);
	}
}
void TestMultiThread() {
	std::thread thread1(MultiThreadAlloc1);
	std::thread thread2(MultiThreadAlloc2);
	thread1.join();
	thread2.join();
}
void BigAlloc() {
	void* p1 = ConcurrentAlloc(257 * 1024);
	ConcurrentFree(p1);

	void* p2 = ConcurrentAlloc(129 * 8 * 1024);
	ConcurrentFree(p2);
}
//int main()
//{
//	//TLSTest();
//	//TestConurrentAlloc2();
//	//TestAddressShift();
//	TestConurrentAlloc1();
//	//TestMultiThread();
//	BigAlloc();
//}